// pages/message/message.js
var app=getApp();
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    imgUrl: app.globalData.imageUrl,
    userInfo: '',
    messageData:[],
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData: true,
    page: 1,
    bottabData: app.globalData.bottabData,
    showRelease:false,
    fbList: app.globalData.fbList,
    isShop: '',
    loadSuccess:false,
    isRead: false
  },
  openPage: function(e){
    if (e.currentTarget.dataset.index=='2'){
      wx.navigateTo({
        url: '/pages/symNotice/symNotice'
      })
    }else{
      wx.navigateTo({
        url: '/pages/notice/notice?index=' + e.currentTarget.dataset.index
      })
    }
    
  },
  releaseTap: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.type == 1 || item.type == 4) {
      if (this.data.userInfo.brandCar) {
        wx.navigateTo({
          url: item.url
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '你还没有完善车辆信息，请先去完善',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: "/pages/perfectInfo/perfectInfo"
              })
            } else {//取消
            }
          }
        })
      }
    } else {
      if (item.url) {
        wx.navigateTo({
          url: item.url
        })
      }
      else {
        if (this.data.isShop == 0) {
          wx.navigateTo({
            url: '/pages/seller/seller'
          })
        }
        else if (this.data.isShop == 1) {
          let isOut = wx.getStorageSync("isOut");
          if (isOut == 1) {
            wx.showModal({
              title: '提示',
              content: '您的店铺已到期，请续费',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/shopIndex/shopIndex?whole=1'
                  })
                }
              }
            })
          }
          else {
            wx.navigateTo({
              url: '/pages/sjwppublish/sjwppublish'
            })
          }
        }
        else if (this.data.isShop == 2) {
          wx.showToast({
            title: '店铺正在审核中，请稍后发布商品',
            icon: 'none',
            duration: 2000
          })
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      ['bottabData[2].current']: true
    })
    this.getMessageData();
    this.getIsShop();
    
    wx.showLoading();
  },
  getIsRead: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGySystemMessage/noneRead',
      data: {
        toUserId: this.data.userInfo.id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          _this.setData({
            isRead: res.data.data
          })
        }
      }
    })
  },
  switchBotab: function (e) {
    if (e.currentTarget.dataset.url) {
      if (e.currentTarget.dataset.index != 2) {
        wx.redirectTo({
          url: e.currentTarget.dataset.url
        })
      }
    } else {
      this.setData({ showRelease: true })
    }
  },
  getIsShop: function () {
    var id = JSON.parse(wx.getStorageSync('userInfo')).id;
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopInfo/getOne',
      data: {
        userId: id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        
        if (!res.data.success) {
          wx.setStorageSync('isShop', '0');
          wx.removeStorageSync('ShopId');
        }
        else {
          wx.setStorageSync('isOut', res.data.data.dateFlag);
          if (res.data.data.data.status == 1) {
            wx.setStorageSync('isShop', '2');
          }
          else {
            wx.setStorageSync('isShop', '1');
            wx.setStorageSync('ShopId', res.data.data.data.id);
          }
        }
        _this.setData({
          isShop: wx.getStorageSync("isShop")
        })
      }
    })
  },
  hideRelease: function (e) {
    this.setData({ showRelease: false })
  },
  showLeave: function (e) {
    var item = e.currentTarget.dataset.obj;
    if (item.infoId) {
      wx.navigateTo({
        url: "/pages/leaveWord/leaveWord?id=" + item.infoId + '&status=' + item.status + '&userId=' + item.userId + '&pulishId=' + item.otherSideId + '&backupA=' + item.backupA
      })
    } else {
      
      wx.navigateTo({
        url: "/pages/leaveWord/leaveWord?id=" + item.userInfo.id + '&status=' + item.status + '&userId=' + item.userId + '&pulishId=' + item.otherSideId + '&backupA=' + item.backupA
      })
    }

  },
  getMessageData(flag){
    var _this=this;
    wx.request({
      url: app.globalData.httpApiCar + 'messageInfo/page', //仅为示例，并非真实的接口地址
      data: {
        otherSideId: this.data.userInfo.id,
        currentPage: this.data.page,
        pageNumber: '10',
        ss:1
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.data.pp.length < 10) {
          _this.setData({
            hasData: false
          })
        } else {
          _this.setData({
            hasData: true
          })
        }
        res.data.data.pp.forEach(function(ele){
          if (ele.createTime){
            console.log(new Date(ele.createTime))
            ele.time = util.getDays(ele.createTime);
            // ele.time = util.getDays(new Date(ele.time.replace(/-/g, "/")).getTime());
          }
          if (ele.messageInfos != undefined && ele.messageInfos.length>0){
            ele.messageInfos.forEach(function(item){
              if (item.createTime) {
                item.time = util.getDays(item.createTime);
              }
            })
          }
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            messageData: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.messageData = _this.data.messageData.concat(res.data.data.pp);
        _this.setData({
          messageData: _this.data.messageData,
          loadMore: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getIsRead();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      page: 1
    })
    this.getMessageData(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasData) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        page: this.data.page + 1
      });
      this.getMessageData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})